/*
* Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
*/
package com.hyyt.shopjsp.util.validate;

import com.hyyt.shopjsp.util.common.EnumUtils;
import com.hyyt.shopjsp.util.common.Utils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.http.ParseException;

import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @author zhx on 2016/8/2 0002.
 */
public class TimeStampPropertyEditor extends PropertyEditorSupport {

    private String pattern = EnumUtils.DataFormat.Y_M_D_H_m_s_S.getValue();;

    private String[] formater_pattern = new String[] { EnumUtils.DataFormat.Y_M_D_H_m_s_S.getValue(),
            EnumUtils.DataFormat.Y_M_D_H_m_s.getValue() };

    /**
     *
     */
    public TimeStampPropertyEditor() {
        // TODO Auto-generated constructor stub
    }

    public TimeStampPropertyEditor(String pattern) {
        // TODO Auto-generated constructor stub
        this.pattern = pattern;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
     */
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if (!Utils.stringIsNotEmpty(text)) {
            setValue(null);
        } else {
            if(text.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
                text+=" 00:00:00";
            }
            try {
                Date date = null;
                try {
                    date = DateUtils.parseDate(text, formater_pattern);
                } catch (java.text.ParseException e) {
                    e.printStackTrace();
                }
                Timestamp timestamp = new Timestamp(date.getTime());
                //				设置转换完的值
                setValue(timestamp);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                setValue(null);
            }
        }

    }

    /*
     * (non-Javadoc)
     *
     * @see java.beans.PropertyEditorSupport#getAsText()
     */
    @Override
    public String getAsText() {
        // TODO Auto-generated method stub
        //	获取model的值
        Timestamp value = (Timestamp) getValue();
        if (value == null) {
            return "";
        } else {
            try {
                Date date = new Date(value.getTime());
                String str = DateFormatUtils.format(date, pattern);
                return str;
            } catch (Exception e) {
                return "";
            }
        }

    }

}